package sk.fiit.icp.settings.view;

import java.io.Serializable;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.yui.calendar.DateField;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.fiit.icp.components.SignInSession;
import sk.fiit.icp.data.Settings;
import sk.fiit.icp.data.SettingsDao;
import sk.fiit.icp.data.Users;
import sk.fiit.icp.data.UsersDao;

public class SettingsPanel extends Panel {
	private RegistrationForm registrationForm;
	private Users user;

	@SpringBean(name = "settingsDao")
	public SettingsDao settingsDao;

	@SpringBean(name = "usersDao")
	public UsersDao usersDao;

	public SettingsPanel(String id) {
		super( id );

		user = ((SignInSession) getSession()).getUser();
		Settings s = settingsDao.findByUser( user );
		if ( s == null ) {
			s = new Settings( user, Integer.valueOf( 0 ), Integer.valueOf( 0 ), Integer.valueOf( 0 ), "", Integer.valueOf( 1 ),
					Integer.valueOf( 1 ) );
		}
		registrationForm = new RegistrationForm( "settingsForm", new CompoundPropertyModel<RegistrationData>(
				new RegistrationData( user, s ) ) );
		add( registrationForm );
	}

	class RegistrationForm extends Form<RegistrationData> {

		public RegistrationForm(String id, CompoundPropertyModel<RegistrationData> model) {
			super( id, model );
			setOutputMarkupId( true );

			final FeedbackPanel feedbackPanel = new FeedbackPanel( "feedback" );
			feedbackPanel.setOutputMarkupId( true );
			add( feedbackPanel );

			add( new CheckBox( "settings.showContactEvents" ) );
			add( new CheckBox( "settings.showContactAvatar" ) );
			add( new TextField<String>( "settings.autoLogoutSecond" ) );
			// add( new TextField<String>( "template" ) );
			// add( new TextField<String>( "notifyPriority" ) );
			add( new TextField<String>( "settings.notifyDaysBefore" ) );

			add( new TextField<String>( "user.email" ) );
			add( new TextField<String>( "user.password" ) );
			add( new DateField( "user.birthdate" ) );
			add( new TextField<String>( "user.address" ) );
			add( new TextField<String>( "user.city" ) );
			add( new TextField<String>( "user.state" ) );
			add( new TextField<String>( "user.phone" ) );

			AjaxSubmitLink link = new AjaxSubmitLink( "link" ) {
				@Override
				protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
					settingsDao.persist( RegistrationForm.this.getModelObject().getSettings() );
					usersDao.update( RegistrationForm.this.getModelObject().getUser() );
					continueToOriginalDestination();
				}

				@Override
				protected void onError(AjaxRequestTarget target, Form<?> form) {
					target.add( feedbackPanel );
				}
			};

			link.add( new Image( "saveImage", new ContextRelativeResource( "/images/Page/ButtonSave.png" ) ) );
			add( link );

		}

		public void setActualEvent(RegistrationData s) {
			registrationForm.setModelObject( s );
		}

	}

	private class RegistrationData implements Serializable {

		Users user;
		Settings settings;

		public RegistrationData(Users u, Settings s) {
			user = u;
			settings = s;
		}

		public Users getUser() {
			return user;
		}

		public void setUser(Users user) {
			this.user = user;
		}

		public Settings getSettings() {
			return settings;
		}

		public void setSettings(Settings settings) {
			this.settings = settings;
		}
	}

}
